from typing import Any, Optional, Dict, Union
from fastapi import status
from pydantic import BaseModel


class BaseResponse(BaseModel):
    """
    基础响应模型 (可被OpenAPI文档识别)
    """
    error: int = 0
    message: str = "success"
    result: Optional[Any] = None


def unified_response(
        *,
        result: Optional[Any] = None,
        error: int = 0,
        message: str = "success",
        status_code: int = status.HTTP_200_OK,
        headers: Optional[Dict[str, str]] = None
) -> Dict[str, Any]:
    """
    通用响应构造器
    :param result: 业务数据内容
    :param error: 错误码(0表示成功)
    :param message: 提示信息
    :param status_code: HTTP状态码
    :param headers: 自定义响应头
    :return: 标准化响应字典
    """
    response_data = {
        "error": error,
        "message": message,
        "result": result
    }

    # 如果是FastAPI路由可直接返回这个字典
    return response_data


# 快捷方法定义
def success_response(
        result: Optional[Any] = None,
        message: str = "success"
):
    """成功响应快捷方法"""
    return unified_response(result=result, message=message)


def error_response(
        message: str = "error",
        error: int = 1,
        status_code: int = status.HTTP_400_BAD_REQUEST
):
    """错误响应快捷方法"""
    return unified_response(
        error=error,
        message=message,
        status_code=status_code
    )
